<?php

class Crontab extends CI_Controller
{

    public function run()
    {
        $result = $this->db->select('cate_name')
            ->get_where(TABLE_API_CATEGORY, array(
            'cate_type' => 2
        ))
            ->result_array();
        foreach ($result as $val) {
            if (stripos($val['cate_name'], 'Detail') === false) { // 生成基础对象模型
                $objecttName = $val['cate_name'];
                $table = 'TABLE_' . strtoupper($val['cate_name']);
                $objectFile = file_get_contents(TEMPLATEPATH . 'ObjectTemplate.tpl');
                $objectFile = str_replace('{ObjectName}', $objecttName, $objectFile);
                $objectFile = str_replace('{TABLE_NAME}', $table, $objectFile);
                if (! file_exists(OBJECTPATH . $objecttName . '.php'))
                    file_put_contents(OBJECTPATH . $objecttName . '.php', $objectFile);
            } else {
                $temp = str_replace('Detail', '', $val['cate_name']);
                $modelName = $temp . 'Model';
                $controllerName = $temp;
                $model = strtolower($temp);
                $modelDetail = $val['cate_name'];
                
                // 生成模型文件
                $modelFile = file_get_contents(TEMPLATEPATH . 'ModelTemplate.tpl');
                $modelFile = str_replace('{ModelName}', $modelName, $modelFile);
                $modelFile = str_replace('{ModelDetail}', $modelDetail, $modelFile);
                $modelFile = str_replace('{model}', $model, $modelFile);
                if (! file_exists(MODELSPATH . $modelName . '.php'))
                    file_put_contents(MODELSPATH . $modelName . '.php', $modelFile);
                    // 生成控制器文件
                $controllerFile = file_get_contents(TEMPLATEPATH . 'ControllerTemplate.tpl');
                $controllerFile = str_replace('{ControllerName}', $controllerName, $controllerFile);
                $controllerFile = str_replace('{ModelName}', $modelName, $controllerFile);
                if (! file_exists(CONTROLLERPATH . $controllerName . '.php'))
                    file_put_contents(CONTROLLERPATH . $controllerName . '.php', $controllerFile);
            }
        }
        echo '自动生成完成';
    }
}